
********************************************************************************	
cd $pathdata_processed
use terminales_fin, clear

sum chance
replace chance = chance/`r(sd)'
replace expected_gpa = expected_gpa/`r(sd)'
replace predicted_gpa = predicted_gpa/`r(sd)'

rename stdteachers_grades teach
rename stdteachers_grades2 teach2
********************************************************************************	    
global controls teach teach2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c 		
		 
global controls2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c 					 
************************************************************************
global partial_gpa i.year_vg3 i.skole_fo

gen all = 1
********************************************************************************
label var chance "\hspace{0.2cm} $widetilde{\textrm{GPA_luck}}$"

label var teach "\hspace{0.2cm} High school course grades"
label var stdms_gpa "\hspace{0.2cm} Middle school GPA" 
label var female "\hspace{0.2cm} Female"
label var age "\hspace{0.2cm} Age"
label var p_age "\hspace{0.2cm} Parents' average age"
label var p_educ "\hspace{0.2cm} Parents' average years of education"
label var ln_p_inc "\hspace{0.2cm} Parents' average log labor income"

label var stdexam_grades "\hspace{0.2cm} Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "\hspace{0.2cm} High school GPA"
label var completion "\hspace{0.2cm} On time HS diploma"
label var hs_gs_c "\hspace{0.2cm} Ever HS diploma"
label var he_e "\hspace{0.2cm} Ever higher education"
label var share_available "\hspace{0.2cm} Share of available HE programs"
label var rank_f_he "\hspace{0.2cm} Selectivity of HE enrollment"
label var bu_sup "\hspace{0.2cm} Number of completed years in HE"
label var first_job "\hspace{0.2cm} Ever employed"
label var ln_wyrkinnt_c1 "\hspace{0.2cm} First job labor income (log)"
label var job10 "\hspace{0.2cm} Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "\hspace{0.2cm} Labor income 8 years after the exams (log)"
********************************************************************************
cd $pathtable	
********************************************************************************
eststo clear
//Summary statistics
estpost summarize stdexam_grades stdvide3 completion hs_gs_c he_e share_available rank_f_he bu_sup first_job ln_wyrkinnt_c1 job10 ln_wyrkinnt_c10
	
esttab using "stats_desc.tex", replace booktabs legend ///
		cells("mean(fmt(3)) sd(fmt(3)) count(fmt(0))") nostar noobs ///
		posthead("Variables & Mean & SD & Observations \\ \midrule" "\multicolumn{1}{l}{\textbf{Outcomes}} \\") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) nomtitle

eststo clear	
estpost summarize teach stdms_gpa female age p_age p_educ ln_p_inc 
	
esttab using "stats_desc.tex", append booktabs legend ///
		cells("mean(fmt(3)) sd(fmt(3)) count(fmt(0))") nostar noobs ///
		posthead("\\ \multicolumn{1}{l}{\textbf{Demographics}} \\") ///
		fragment label mgroup(none) nonumbers collabels(none) eqlabels(none) nomtitle
eststo clear					
*******************************************************************************	
label var chance "$widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa "$\textrm{GPA_luck}$"

label var stdexam_grades "Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "High school GPA"
label var completion "On time HS diploma"
label var hs_gs_c "Ever HS diploma"
label var he_e "Ever higher education"
label var share_available "Share of available HE programs"
label var rank_f_he "Selectivity of HE enrollment"
label var bu_sup "Number of completed years in HE"
label var first_job "Ever employed"
label var ln_wyrkinnt_c1 "First job labor income"
label var job10 "Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "Labor income 8 years after the exams"
*******************************************************************************			
//Table 1
areg predicted_gpa chance $controls i.year_vg3 if all == 1, cluster(group) rob absorb(skole_fo)
test chance
		
foreach sample in all {
	foreach var in stdexam_grades stdvide3 stdadmission_score_vg3 completion hs_gs_c {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
		
foreach sample in all {
	foreach var in he_e {
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
			
foreach sample in all {
	foreach var in share_available rank_f_he bu_sup {
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}			
								
foreach sample in all {
	foreach var in first_job ln_wyrkinnt_c1 job10 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob 
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				}
			}
			
		local sample "all"
		esttab `sample'stdexam_grades `sample'stdvide3 `sample'completion `sample'hs_gs_c using "mainRF.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\textit{Panel A: High school Outcomes} & Exam grades & High school GPA & On time & Ever \\" "& in 3\textsuperscript{rd} year & & HS diploma & HS diploma \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
					
		esttab `sample'he_e `sample'share_available `sample'rank_f_he `sample'bu_sup using "mainRF.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\textit{Panel B: Higher Education Outcomes} & Ever & Share of available & Selectivity & Number of completed \\" "& higher education & HE programs & of HE enrollment & years in HE \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		esttab `sample'first_job `sample'ln_wyrkinnt_c1 `sample'job10 `sample'ln_wyrkinnt_c10 using "mainRF.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\textit{Panel C: Labor Market Outcomes} & Ever & First job annual & Employed 8 years & Annual labor income \\" "& employed & labor income & after the exams & 8 years after the exams \\" "& & (log) & & (log) \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		eststo clear
********************************************************************************
//IV
*Labor market outcomes 
foreach sample in all {
	foreach var in ln_wyrkinnt_c1 ln_wyrkinnt_c10 {		
		
				eststo `sample'`var': ivregress 2sls `var' (stdvide3 = chance) $controls2 $partial_gpa if `sample' == 1, cluster(group) rob 				
				su `var' if e(sample) 
				estadd r(mean): `sample'`var'
				
				reg stdvide3 chance $controls2 $partial_gpa if `sample' == 1 & !missing(`var'), cluster(group) rob 
				test chance
				estadd scalar Fstat = r(F): `sample'`var'
				}
			}	
		 
foreach sample in all {
	foreach var in ln_wyrkinnt_c1 ln_wyrkinnt_c10 {
		
	eststo `sample'`var'ols: reg `var' stdvide3 $controls2 $partial_gpa if `sample' == 1, cluster(group) rob 
	}
}			

	local sample "all"
	esttab `sample'ln_wyrkinnt_c1 `sample'ln_wyrkinnt_c1ols `sample'ln_wyrkinnt_c10 `sample'ln_wyrkinnt_c10ols using "mainIV.tex", replace booktabs legend ///
		keep(stdvide3) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& \multicolumn{4}{c}{Outcomes} \\" "\cmidrule(lr){2-5}" "& First job log annual & First job log annual & Log annual labor income & Log annual labor income \\" "& labor income & labor income & 8 years after the exams & 8 years after the exams \\" "& (2SLS) & (OLS) & (2SLS) & (OLS) \\" "\midrule") ///
		fragment stats(N, label("N") fmt(0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		
		eststo clear
********************************************************************************	
//Fields of study
foreach sample in all {
	foreach var in general_intro_course humanities social_science teaching health science enigneering technology business law medicine other {
			 eststo `sample'`var': ivregress 2sls `var'_f_he (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob
			 su `var' if e(sample) 
			 estadd r(mean): `sample'`var'
				}
			}
			
		local sample "all"
		esttab `sample'humanities `sample'social_science `sample'teaching `sample'health `sample'science `sample'enigneering `sample'technology `sample'business `sample'law `sample'medicine using "fields_study.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& Humanities & Social Science & Teaching & Health & Science & Engineering & Technology & Business & Law & Medicine \\" "\midrule" "\multicolumn{5}{l}{\textit{Sample of students who enrolled in college}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		eststo clear		
		
foreach sample in all {
	foreach var in general_intro_course humanities social_science teaching health science enigneering technology business law medicine other {
			 eststo `sample'`var': ivregress 2sls `var'_f_he (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1 & (bu_sup <= 3), cluster(group) rob
			 su `var' if e(sample) 
			 estadd r(mean): `sample'`var'
				}
			}
		local sample "all"
		esttab `sample'humanities `sample'social_science `sample'teaching `sample'health `sample'science `sample'enigneering `sample'technology `sample'business `sample'law `sample'medicine using "fields_study.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{5}{l}{\textit{Subsample with a maximum of 3 years of completed higher education}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		eststo clear					

foreach sample in all {
	foreach var in general_intro_course humanities social_science teaching health science enigneering technology business law medicine other {
			 eststo `sample'`var': ivregress 2sls `var'_f_he (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1 & (bu_sup > 3 & !missing(bu_sup)), cluster(group) rob
			 su `var' if e(sample) 
			 estadd r(mean): `sample'`var'
				}
			}	
			
		local sample "all"
		esttab `sample'humanities `sample'social_science `sample'teaching `sample'health `sample'science `sample'enigneering `sample'technology `sample'business `sample'law `sample'medicine using "fields_study.tex", append booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{5}{l}{\textit{Subsample with more than 3 years of completed higher education}} \\") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		eststo clear						
********************************************************************************
foreach i of numlist 0/5 {
	gen bu_sup`i' = bu_sup <= `i'
} 

foreach sample in all {
	foreach var in bu_sup0 bu_sup1 bu_sup2 bu_sup3 bu_sup4 bu_sup5 {
			 eststo `sample'`var': ivregress 2sls `var' (predicted_gpa = chance) $controls $partial_gpa if `sample' == 1, cluster(group) rob
			 su `var' if e(sample) 
			 estadd r(mean): `sample'`var'
				}
			}
			
		local sample "all"
		esttab `sample'bu_sup0 `sample'bu_sup1 `sample'bu_sup2 `sample'bu_sup3 `sample'bu_sup4 `sample'bu_sup5 using "bu_sup.tex", replace booktabs legend ///
		keep(predicted_gpa) cells(`"b(star fmt(4))"' `"se(fmt(4) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("& No completed years & $\leq$ 1 year & $\leq$ 2 years & $\leq$ 3 years & $\leq$ 4 years & $\leq$ 5 years \\" "& of HE & & & & & \\" "\midrule") ///
		fragment stats(mean N, label("Mean dep. var." "N") fmt(%9.3f 0)) label mgroup(none) nonumbers collabels(none) eqlabels(none) mlabel(none) 
		eststo clear	

